package middleware

import (
	"fmt"
	"gin-wheel/global"
	"gin-wheel/pkg/app"
	"gin-wheel/pkg/errcode"
	"runtime/debug"
	"time"

	"github.com/gin-gonic/gin"
)

func Recovery() gin.HandlerFunc {
	return func(c *gin.Context) {
		defer func() {
			if err := recover(); err != nil {
				s := fmt.Sprintf("异常抛出, 发生时间: %d, 错误信息: %v, stack: %v", time.Now().Unix(), err, string(debug.Stack()))
				global.ZapLog.Error(s)
				app.NewResponse(c).ToResponse(errcode.ServerError, nil)
				return
			}
		}()
		c.Next()
	}
}
